import java.util.HashMap;
import java.util.Map;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

// BankDatabase.java
// Represents the bank account information database

public class BankDatabase
{
  private Map<String,Account> accounts; // array of Accounts
  
  
   // no-argument BankDatabase constructor initializes accounts
   public BankDatabase()
   {
      accounts = new HashMap<String,Account>(); // just 2 accounts for testing
      accounts.put("pape11ad", new Account( "81dc9bdb52d04dc20036dbd8313ed055", 200, 300));      
      accounts.put("raha11ad", new Account( "81dc9bdb52d04dc20036dbd8313ed055", 100, 200));      
   } // end no-argument BankDatabase constructor
   
   // retrieve Account object containing specified account number
   private Account getAccount( String accountKey )
   {
	   try {
		   return accounts.get(accountKey);
	} catch (Exception e) {
		System.out.println(e);
		// TODO: handle exception
	} 
      return null; // if no matching account was found, return null
   } // end method getAccount

   //Hentet fra http://www.asjava.com/core-java/java-md5-example/
   public String getMD5(String input) {
       try {
           MessageDigest md = MessageDigest.getInstance("MD5");
           byte[] messageDigest = md.digest(input.getBytes());
           BigInteger number = new BigInteger(1, messageDigest);
           String hashtext = number.toString(16);
           // Now we need to zero pad it if you actually want the full 32 chars.
           while (hashtext.length() < 32) {
               hashtext = "0" + hashtext;
           }
           return hashtext;
       }
       catch (NoSuchAlgorithmException e) {
           throw new RuntimeException(e);
       }
   }

   public boolean authenticateUser( String accountName, String accountPassword )
   {
      // attempt to retrieve the account with the account number
	   
      Account userAccount = getAccount( accountName );
      
      //krypterer password
      String digestPassword = getMD5(accountPassword);
      
      // if account exists, return result of Account method validatePIN
      if ( userAccount != null )
         return userAccount.validatePassword( digestPassword);
      else
         return false; // account number not found, so return false
   } // end method authenticateUser

   // return available balance of Account with specified account number
   public double getAvailableBalance( String accountName )
   {
      return getAccount( accountName ).getAvailableBalance();
   } // end method getAvailableBalance

   // return total balance of Account with specified account number
   public double getTotalBalance( String accountName )
   {
      return getAccount( accountName ).getTotalBalance();
   } // end method getTotalBalance

   // credit an amount to Account with specified account number
   public void credit( String accountName, double amount )
   {
      getAccount( accountName ).credit( amount );
   } // end method credit

   // debit an amount from Account with specified account number
   public void debit( String accountName, double amount )
   {
      getAccount( accountName ).debit( amount );
   } // end method debit
} // end class BankDatabase



/**************************************************************************
 * (C) Copyright 1992-2010 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/